Uec3.BuyPackageWindow = Ext.extend(Ext.app.Module,{
	id: 'buyPackage',
   /*
    * Initialize this module.
    * This function is called at startup (page load/refresh).
    */
   init : function(initParams){
   
   },
 
   detailPackage:function(packageId){
   	  var detailPackage_grid = this.curWindow.get(0).get(2);
   	  detailPackage_grid.getStore().load({
   	             params:{queryParam:packageId},
   	             callback:function(r,options,success){
   	             	if(!success){
     	             	Ext.MessageBox.alert("提示",this.reader.jsonData.msg);
   	             	}
   	             }
   	  });     
   },
   
   buyPackage : function(packageId){  
   	    var _this = this;
   	    var companyType = _this.curWindow.get(0).get(1).getStore().reader.jsonData.companyType;
   	    if(companyType == 2){
   	       //对汽修厂显示可选择市场  	    
   	       _this.app.desktop.launchWindowByParam("market4RepairStore",{packageId:packageId});  	      
   	    }else{
   	    	Ext.MessageBox.confirm("提示","确定购买此套餐?",function(btnId){
   	    	if(btnId=='yes'){
   	    	   var myMask = new Ext.LoadMask(_this.curWindow.el,{msg:'正在处理请求,请稍后.....'});
   	    	   myMask.show();
   	    	   Ext.Ajax.request({		            
				      url:Uec3.App.Constants.homeUrl+"/companyPackage/buyPackage.do",
				      method :"post",					   
				      params:{queryParam:Ext.encode({packageId:packageId})},
				      timeout:360000,
				      callback :function(options,success,response){
				    	  
                      },
                      success:function(response,options){
                      	 	var resp = Ext.decode(response.responseText);
				         	Ext.Msg.alert("提示",resp.msg);
				         	if(resp.success){
				         		//window.location.href= Uec3.App.Constants.homeUrl+ '/desktop/indexNew.do';
				         	}
				         	myMask.hide();
                      },
                      failure:function(response,options){
                         	Ext.Msg.alert("提示","发生异常,购买套餐失败,请稍后再试!");
                         	myMask.hide();
                      }
		       }); 
   	    	}  	    	
   	    }); 	    
      }  	   
   },
   
   getComponent:function(){
   	    
   	    
   	    var package_store = new Ext.data.JsonStore({
   	          url:Uec3.App.Constants.homeUrl + '/companyPackage/searchPackage.do',
   	          root:'root',   	         
   	          fields:['id','name','price']
   	         /* listeners:{
   	            'beforeload':function(){
                    Ext.MessageBox.wait("正在加载数据...","提示");
                 }
   	          }*/
   	    });
   	
   	    var selModel = new Ext.grid.RowSelectionModel({
   	           moveEditorOnEnter:true,
   	           singleSelect :true
   	    });
   	    
   	    var actionCol = {
					header : "操作",					
					sortable : false,
					dataIndex:'id',
					renderer : this.moduleActionRenderer([
					           {func : "detailPackage",desc : "查看详情"},
					           {func : "buyPackage",desc : "购买"}
		            ])
		};
   	    
   	    var colModel = new Ext.grid.ColumnModel({
   	         columns:[
          		   	new Ext.grid.RowNumberer(),        		   	
      		 	   	{header:'套餐名称',dataIndex:'name',width:120},
      		 		{header:'价格',dataIndex:'price',width:80},
      		 		actionCol
          		],
          		defaults: {
          			menuDisabled: true         
       			}
   	    });
   	    
   		var package_grid = new Ext.grid.GridPanel({
   		     viewConfig:{forceFit:true,scrollOffset:20},	   	   
	   	   	 autoScroll : true,
	   	   	 loadMask:{mag:'套餐列表加载中,请稍后....'},
	   	   	 titleCollapse:true,
	         colModel:colModel,
	         height:250,
	         selModel:selModel,
	         store:package_store,	           
	         columnLines :true
	        /* bbar: new Ext.PagingToolbar({
		            pageSize:10,
	                store:package_store,
	                displayInfo: true,
	                displayMsg: '第 {0} - {1} 条  共 {2} 条',
                    emptyMsg: "没有记录",                   
                    listeners:{
                        beforechange:function(){
                      		 Ext.MessageBox.wait("正在加载数据...","提示");	                     
                        },
                        change:function(){
                            Ext.MessageBox.hide();
                        }
                    }
		     }) */
   		});  	
   		
   		var detail_package_store = new Ext.data.JsonStore({
   		    url:Uec3.App.Constants.homeUrl + '/companyPackage/searchPackageService.do',
   		    root:'root',
   		    fields:['serviceName','duration']
   		});
   		
   		var detail_package_colModel = new Ext.grid.ColumnModel({
   		    columns:[
          		   	new Ext.grid.RowNumberer(),        		   	
      		 	   	{header:'套餐内容',dataIndex:'serviceName',width:120},
      		 		{header:'有效时长(月)',dataIndex:'duration',width:80}    		 	
            ],
            defaults: {menuDisabled: true}
   		});
   		
   		var detail_package_grid = new Ext.grid.GridPanel({
   			 title:'套餐详情',
   			 loadMask:{mag:'套餐详情加载中,请稍后....'},
   		     viewConfig:{forceFit:true,scrollOffset:20},	   	   
	   	   	 autoScroll : true,
	   	   	 titleCollapse:true,
	         colModel:detail_package_colModel,	       
	         height:250,
	         store:detail_package_store,	           
	         columnLines :true 
   		});
   		
   		var label = new Ext.form.Label({
                text:'您的账户余额：',
                style: {font:'normal 13px arial,serif'}
        });
   		
   	    package_store.load({
   	        callback:function(records,options,success){
   	        	Ext.MessageBox.hide();  	        	
   	        	if(success){	              	 
	                 var total=records.length;
	                 if(!total){
	                    Ext.MessageBox.alert("提示","未查询到相关套餐列表!");
	                 }
	                 label.setText("您的账户余额："+this.reader.jsonData.balance);
	           }else{
	           		Ext.MessageBox.alert("提示",this.reader.jsonData.msg);
	           }
   	        }
   	    });
   		
   	    var panel = new Ext.Panel({
   	       items:[label,package_grid,detail_package_grid]
   	    });
   	    
   		return panel;
   },
   
   /**
	* Create this modules window here.
	*/
   createWindow : function(params){
    	var desktop = this.app.getDesktop();
        var win = desktop.getWindow('buyPackage');
        if (!win) {
			win = desktop.createWindow({
				        id : "packageAdd",
						title : '购买套餐',
						width : 500,
						height : 500,
						iconCls : 'acc-icon',
					    minWidth:1000,
						maximizable : true,
						resizable:false,
						maximized:false,
						//autoScroll : true,
						constrainHeader : true,
						taskbuttonTooltip : "购买套餐",
						layout : "fit",
						items : [this.getComponent(params)]
					});
		}		
    	win.show();         
   }
});